package com.leyantech.ecp.agenx.common.di;

import com.leyantech.utility.constructs.Constructs;

import com.ctrip.framework.apollo.ConfigService;
import com.google.inject.AbstractModule;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.TransportMode;

/**
 * @author eahau, {@literal <eahau@foxmail.com>}
 * @date 2022-11-15.
 */
public class RedissonModule extends AbstractModule {

  public static final com.ctrip.framework.apollo.Config APOLLO_CONFIG = ConfigService
      .getConfig("redis");

  public static final RedissonClient redissonClient = provideRedissonClient();

  @Override
  protected void configure() {

    bind(RedissonClient.class).toInstance(redissonClient);
  }

  static RedissonClient provideRedissonClient() {

    final String password = APOLLO_CONFIG.getProperty("password", null);
    final String address = APOLLO_CONFIG.getProperty("address", "redis://127.0.0.1:6379");

    final String procName = Constructs.getProcNameOrDefault();

    final Config config = new Config();
    config.setNettyThreads(APOLLO_CONFIG.getIntProperty(procName + ".threadSize", 8))
        .setTransportMode(TransportMode.NIO)
        .useSingleServer()
        .setAddress(address)
        .setPassword(password);
    return Redisson.create(config);
  }

}